import { getSongUrl, getSongDetail, checkMusic } from "@/api/api"

const state = {
  songid: '',
  songinfo: '',
  playrecord: JSON.parse(localStorage.getItem("playRecord")) || [], // 歌曲播放记录
}

const mutations = {
  SET_SONGID: (state, songid) => {
    state.songid = songid
  },
  SET_SONGINFO: (state, songinfo) => {
    state.songinfo = songinfo
  },
  SET_PLAYRECORD: (state, playrecord) => {
    state.playrecord = playrecord
  },
}

const actions = {
  async judgeMusic(context, playload) {
      return new Promise((resolve) => {
        let param = {
          id: playload
        }
        let param1 = {
          ids: playload
        }
        Promise.all([
          getSongUrl(param),
          checkMusic(param),
          getSongDetail(param1)
        ]).then(([songUrl, checkmusic, songInfo]) => {
          if (songUrl.data.code == 200 && checkmusic.data.success
              && songInfo.data.code == 200) {
              let { data: { data: [songurl] } } = songUrl
              songInfo = songInfo.data
              songInfo.songurl = songurl
              songInfo.checkmusic = checkmusic.data.message
              context.commit('SET_SONGINFO', songInfo)
              context.commit('SET_SONGID', songurl.id)
              resolve(songurl.id)
          }
        })
      })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
